﻿cbuffer cbPerObject
{
	float4x4 WorldViewProjection;	
	float4x4 WorldView;	
	float4x4 View;	
	float4x4 InverseProjection;
}
cbuffer cbGlobal
{
	float zFar;
}

struct App2VS
{
	float3 position : POSITION;
	float3 color : COLOR;
	float3 normal : NORMAL;
	//float2 uv : TEXCOORD0;
};

struct VS2PS
{
	float4 pos : SV_Position;
	float depth : PixDepth;
};
struct PS2GPU
{
	float2 vsmValue : SV_Target0;
	//float depth : SV_TARGET0;
};
void vs(App2VS IN, out VS2PS OUT)
{
	OUT.pos = mul(float4(IN.position, 1), WorldViewProjection);
	OUT.depth = OUT.pos.z;
}

void ps(VS2PS IN, out PS2GPU OUT)
{
	
	//VSM
	{
		float expConst = 12;
		float scaledDepth =  IN.depth / zFar;
		float expDepth = exp(expConst * scaledDepth);
		OUT.vsmValue = float2(expDepth, expDepth * expDepth);
		//OUT.vsmValue = float2(exp(scaledDepth), scaledDepth * scaledDepth);
		//OUT.vsmValue = float2(scaledDepth, scaledDepth * scaledDepth);
	}
	
	//OUT.depth = IN.depth / zFar;
}

BlendState DepthBlend
{ 
    BlendEnable[0] = False;
};
RasterizerState AARasterizer
{
	CullMode = NONE;
	FillMode = SOLID;
	MultisampleEnable = TRUE;
};
technique10 Render
{
	pass P0
	{
		SetBlendState(DepthBlend, float4(1,1,1,1), 0xFFFFFFFF);
		SetRasterizerState(AARasterizer);
		SetGeometryShader( 0 );
		SetVertexShader( CompileShader( vs_4_0, vs() ) );
		SetPixelShader( CompileShader( ps_4_0, ps() ) );
	}
}